#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Python 3.x
SQLAlchemy 1.4.41

通过SQLAlchemy执行原生SQL
"""
from sqlalchemy import create_engine
from constants import DB_URI

# 连接数据库
engine = create_engine(DB_URI, echo=True)

# 使用with语句连接数据库，如果发生异常会被捕获
with engine.connect() as con:
    # 先删除users表
    con.execute('drop table if exists users')
    # 创建一个users表，有自增长的id和name
    con.execute('create table users(id int primary key auto_increment, name varchar(25))')
    # 插入两条数据到表中
    con.execute('insert into users(name) values("abc")')
    con.execute('insert into users(name) values("xiaotuo")')
    # 执行查询操作
    results = con.execute('select * from users')
    # 从查找的结果中遍历
    for result in results:
        print(result)
